package com.fastjrun.eladmin.itrms.service.impl;

import com.fastjrun.eladmin.itrms.dao.RmsProjectEnvironmentDao;
import com.fastjrun.eladmin.itrms.dto.ProjectEnvironmentDTO;
import com.fastjrun.eladmin.itrms.dto.ProjectEnvironmentQueryDTO;
import com.fastjrun.eladmin.itrms.entity.RmsProjectEnvironment;
import com.fastjrun.eladmin.itrms.entity.RmsProjectEnvironmentExample;
import com.fastjrun.eladmin.itrms.mapstruct.RmsProjectEnvironmentMapper;
import com.fastjrun.eladmin.itrms.service.RmsProjectEnvironmentControllerService;
import com.fastjrun.eladmin.itrms.utils.PageUtils;
import com.fastjrun.eladmin.itrms.vo.ProjectEnvironmentVO;
import com.fastjrun.eladmin.vo.PageResult;
import com.github.pagehelper.PageInfo;
import me.zhengjie.utils.SecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service("rmsProjectEnvironmentControllerService")
public class RmsProjectEnvironmentControllerServiceImpl extends BaseItrmsService implements RmsProjectEnvironmentControllerService {


    @Resource
    RmsProjectEnvironmentDao rmsProjectEnvironmentDao;
    @Resource
    RmsProjectEnvironmentMapper rmsProjectEnvironmentMapper;

    private RmsProjectEnvironmentExample retrive(Long projectId, ProjectEnvironmentQueryDTO queryDTO) {
        log.debug("queryDTO={}", queryDTO);
        RmsProjectEnvironmentExample rmsProjectEnvironmentExample = new RmsProjectEnvironmentExample();
        RmsProjectEnvironmentExample.Criteria criteria = rmsProjectEnvironmentExample.createCriteria();
        criteria.andProjectIdEqualTo(projectId);
        if (queryDTO != null) {
            if (!StringUtils.isBlank(queryDTO.getEnvironmentName())) {
                criteria.andEnvironmentNameLike(queryDTO.getEnvironmentName());
            }
        }
        return rmsProjectEnvironmentExample;
    }

    @Override
    public PageResult<ProjectEnvironmentVO> query(Long projectId, ProjectEnvironmentQueryDTO queryDTO, Pageable pageable) {
        if (pageable == null) {
            pageable = PageRequest.of(PAGE_CURRENT, PAGE_SIZE);
        }


        PageUtils.startPage(pageable);
        RmsProjectEnvironmentExample rmsProjectEnvironmentExample=this.retrive(projectId,queryDTO);
        String orderClause=this.getOrderClause(pageable);
        if(StringUtils.isNotBlank(orderClause)){
            rmsProjectEnvironmentExample.setOrderByClause(orderClause);
        }

        PageInfo<RmsProjectEnvironment> pageInfo = new PageInfo<>(rmsProjectEnvironmentDao.selectByExampleWithBLOBs(rmsProjectEnvironmentExample));
        List<ProjectEnvironmentVO> list = rmsProjectEnvironmentMapper.toVO(pageInfo.getList());

        PageResult<ProjectEnvironmentVO> pageResult = new PageResult<>();
        pageResult.setContent(list);
        pageResult.setTotalElements((int) pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public void create(ProjectEnvironmentDTO createDTO) {
        RmsProjectEnvironment rmsProjectEnvironment=this.rmsProjectEnvironmentMapper.toEntity(createDTO);
        rmsProjectEnvironment.setCreateUser(SecurityUtils.getCurrentUsername());
        rmsProjectEnvironment.setCreateUserId(SecurityUtils.getCurrentUserId());
        rmsProjectEnvironment.setOwnerUser(sysUserDao.selectByPrimaryKey(createDTO.getOwnerUserId()).getUsername());
        rmsProjectEnvironment.setCreateDate(new Date());
        rmsProjectEnvironment.setUpdateDate(new Date());
        this.rmsProjectEnvironmentDao.insert(rmsProjectEnvironment);

    }

    @Override
    public void update(ProjectEnvironmentDTO updateDTO) {

        RmsProjectEnvironment rmsProjectEnvironment=this.rmsProjectEnvironmentMapper.toEntity(updateDTO);
        rmsProjectEnvironment.setOwnerUser(sysUserDao.selectByPrimaryKey(updateDTO.getOwnerUserId()).getUsername());
        rmsProjectEnvironment.setUpdateDate(new Date());
        this.rmsProjectEnvironmentDao.updateByPrimaryKeySelective(rmsProjectEnvironment);

    }

    @Override
    public void delete(Long[] ids) {
        RmsProjectEnvironmentExample rmsProjectEnvironmentExample = new RmsProjectEnvironmentExample();
        rmsProjectEnvironmentExample.createCriteria().andIdIn(Arrays.asList(ids));
        this.rmsProjectEnvironmentDao.deleteByExample(rmsProjectEnvironmentExample);

    }
}
